import {Component, OnInit} from '@angular/core';
import {
  ${TableName!}Service
} from "./service/${table\-name!}-service";
import {${TableName!}} from "./entity/${table\-name!}";
import {NzNotificationService} from "ng-zorro-antd/notification";

@Component({
  selector: 'app-${table\-name!}',
  templateUrl: './${table\-name!}.component.html',
  styleUrls: ['./${table\-name!}.component.scss']
})
export class ${TableName!}Component implements OnInit {
  constructor(private ${tableName!}Service: ${TableName!}Service, private notificationService: NzNotificationService) {
  }
  modalConfig = {
    visiable: false,
    record: {},
  }
  queryParam: ${TableName!} = {
  }
  table: any = {
    dataSet: [],
    loading: false,
    page: {
      total: 0,
      pageIndex: 1,
      pageSize: 10,
      show: true,
    },
  }

  ngOnInit() {
    this.doSearch()
  }


  doSearch() {
    this.table.loading = true
    const param = {
      page: this.table.page.pageIndex - 1,
      size: this.table.page.pageSize,
    }
    this.${tableName!}Service.page(param.page, param.size, this.queryParam).subscribe({
        next: res => {
          this.table.loading = false
          this.table.dataSet = res.result.content
          this.table.page.total = res.result.totalElements

        },
        error: err => {
          console.error(err)
          this.table.loading = false
          this.notificationService.warning("warning", "request fail")
        }
      }
    )
  }

  doEdit(data: any) {
    this.modalConfig.record = {...data}
    this.modalConfig.visiable = true
  }

  doRemove(data: any) {
    this.${tableName!}Service.remove(data.id).subscribe(res => {
      if (res.status == 200) {
        this.doSearch();
        this.notificationService.success("Success", "delete Success")
      }
    })
  }

  doAdd() {
    this.modalConfig.record = {}
    this.modalConfig.visiable = true
  }

  pageChange(index: number) {
    this.table.page.pageIndex = index
    this.doSearch()
  }
}
